var express = require('express');
var router = express.Router();

var Product = require('../models/product.js')

/* GET users listing. */
router.get('/list', function (req, res, next) {
    Product.find(function (err, docs) {
        if (err) {
            console.log(err)
        } else {
            res.send(docs)
        }
    })
});


router.post('/addProduct', function (req, res) {
    var image = {
        'cdnUri': 'http://y1.ifengimg.com/3e39b4f8d466c17c/2014/0316/rdn_53256a4adde13.jpg',
        'files': ['file 0', 'file 1'],
        'descript': ['descript 0', 'descript 1']
    }
    var product = new Product({
        'title': '牙膏',
        'tags': ['a','b','c'],
        'image': image,
        'price': 12.0
    })
    Product.insert(product,function (err) {
        if (err) {
            console.log(err)
        } else {
            res.send('docs')
        }
    })
});

router.post('/delete', function (req, res) {
    var condition = {
        'title': '牙膏'
    }
    Product.findOneAndRemove(condition, function (err) {
        if (err) {
            console.log(err)
        } else {
            condition.status = '删除成功'
            res.send(condition)
        }
    })
})

router.post('/find', function (req, res, next) {
    var condition = {
        'title': '牙膏'
    }
    Product.find(condition, function (err) {
        if (err) {
            console.log(err)
        } else {
            condition.status = '查找成功'
            res.send(condition)
        }
    })
})

router.post('/update', function (req, res, next) {
    var condition = {
        'title': '牙膏'
    },
        option = {
            'price': 25.0
        };

    Product.findOneAndUpdate(condition, function (err) {
        if (err) {
            console.log(err)
        } else {
            option.status = '修改成功'
            res.send(option)
        }
    })
})

module.exports = router;